﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post;
using sexhealth.console.service.post.vo;
using System.Text;
using com.pro2e.web.util.page;
public partial class admin_UC04_0402channelmaintain : System.Web.UI.Page
{
    PostService postService = new PostService();
    int channelId = 7;
    private int ChannelMode
    {
        get { if (ViewState["chmode"] == null) { ViewState["chmode"] = 0; } return int.Parse(ViewState["chmode"].ToString()); }
        set { ViewState["chmode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showListMode();
            GetList();
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ChannelMode = 0;
        showModifyMode();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        showListMode();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ChannelMode == 0)
        {
            ChannelVO ch = new ChannelVO();
            ch.DescriptionHtmlContent = ckbDescription.value;
            ch.Name = txtName.Text.Trim();
            ch.ParentId = channelId;
            postService.InsertChannel(ch);

        }
        else
        {
            ChannelVO ch = postService.DaoGetVOById<ChannelVO>(ChannelMode);
            ch.Name = txtName.Text.Trim();
            ch.DescriptionHtmlContent = ckbDescription.value;
            postService.UpdateChannel(ch);
        }
        showListMode();
        GetList();

    }
    private void showListMode()
    {
        pnlGv.Visible = true;
        pnlContent.Visible = false;
        btnAddNew.Visible = true;
    }
    private void showModifyMode()
    {
        pnlGv.Visible = false;
        pnlContent.Visible = true;
        btnAddNew.Visible = false;
    }
    private void GetList()
    {

        gvResult.DataSource = postService.GetChannelList(channelId);
        gvResult.DataBind();

    }
    protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int chNo = int.Parse(e.CommandArgument.ToString());
        ChannelVO ch = postService.DaoGetVOById<ChannelVO>(chNo);
        switch (cmdName)
        {
            case "myModify":

                txtName.Text = ch.Name;
                ckbDescription.value = ch.DescriptionHtmlContent;
               
                showModifyMode();
                ChannelMode = chNo;
                break;

            case "myDel":
                postService.DaoDelete(ch);
                GetList();
                break;

            default:
                break;
        }
    }
}
